<!-- 每日报告 -->
<template>
  <SearchTable :query="data.query" :list="data.list" :total="data.total" :showExport="true"  @changeSize="changeSize"
    @changePage="changePage" @search="getSearchList" @download="exportXlsx" @changeTh="data.dialogVisible = true" >
    <template #form>
      <el-form-item :label="$t('39')">
        <el-date-picker v-model="data.query.date" type="daterange" range-separator="-" :start-placeholder="$t('26')"
          :end-placeholder="$t('27')" value-format="YYYY-MM-DD" @change="dateChange" :disabled-date="indexStore.disabledDateTime"/>
        <WeekDate @checkDate="checkDate" :index="data.index"></WeekDate>
      </el-form-item>
    </template>
    <template #button></template>
    <template #table>
      <template v-for="(item, index) in data.fields" :key="index">
        <el-table-column v-if="item.visible" :prop="item.prop" :label="item.label" :width="item.width"
          show-overflow-tooltip>
          <template #header>
            <el-tooltip v-if="item.tips" effect="dark" placement="top">
              <template #content>
                <div v-html="item.tips"></div>
              </template>
              <el-icon class="ques-tips">
                <QuestionFilled />
              </el-icon>
            </el-tooltip>
            {{ item.label }}
          </template>
          <template v-if="item.prop == 'date'" #default="scope">
            {{ setTimestampTime(scope.row.date).substring(0,11) }}
          </template>
          <template v-if="item.prop == 'game_bet'" #default="scope">
            <span class="colorText">{{ BigWan(scope.row.game_bet) }}</span>
          </template>
          <template v-if="item.prop == 'game_win'" #default="scope">
            <span class="colorText">{{ BigWan(scope.row.game_win) }}</span>
          </template>
          <template v-if="item.prop == 'game_income'" #default="scope">
            <span :class="colorClass(scope.row.game_income)">{{ BigWan(scope.row.game_income) }}</span>
          </template>
        </el-table-column>
      </template>
    </template>
  </SearchTable>

  <!-- 变更表头弹框 -->
  <tableHeader :dialogVisible="data.dialogVisible" :list="data.fields" @cancel="data.dialogVisible = false"
    @resetData="resetData"></tableHeader>
</template>

<script lang="ts" setup>
import { ref, reactive, onMounted } from 'vue'
import { getAgentDailyReport } from '@/api/report'
import tableHeader from '@/components/tableHeader/tableHeader.vue'
import SearchTable from '@/components/SearchTable/SearchTable.vue'
import WeekDate from '@/components/weekDate/weekDate.vue'
import { setTimeValue, setTruncationTime, setTimestampTime, getMonth } from '@/utils/time'
import { BigWan } from '@/utils/math'
import { colorClass } from '@/utils/index'
import { exportToExcel } from '@/utils/export'

import { useI18n } from 'vue-i18n'
const { t } = useI18n()
import { useStore } from '@/stores/index'
const indexStore = useStore()

const data = reactive({
  query: {
    start_time: '', // 开始时间
    end_time: '', // 结束时间
    date: getMonth(0),
    page: 1,
    size: 50
  },
  list: [],
  total: 0,
  fields: [
    { label: t('70'), prop: 'date', width: '150', visible: true },
    { label: t('71'), prop: 'game_bet', width: '', visible: true, tips: t('317') },
    { label: t('72'), prop: 'game_win', width: '', visible: true, tips: t('318') },
    { label: t('73'), prop: 'game_income', width: '', visible: true, tips: t('319', { br: '<br>' }) },
    { label: t('74'), prop: 'valid_user', width: '', visible: true, tips: t('320') },
    { label: t('75'), prop: 'new_user', width: '', visible: true, tips: t('321') }
  ],
  dialogVisible: false,
  index: 3
})

onMounted(async () => {
  let day = new Date().getDay();
  getSearchList()
})

const getSearchList = async () => {
  indexStore.setPageLoading(true)
  data.query = setTimeValue(data.query)
  let res = await getAgentDailyReport(data.query)
  indexStore.setPageLoading(false)
  data.list = res.data.list
  data.total = res.data.total
}

const checkDate = (val: never[]) => {
  data.query.date = val;
  getSearchList()
}
const dateChange = () => {
  data.index = 0
}

// 修改页数
const changePage = (val: number) => {
  data.query.page = val
  getSearchList()
}
const changeSize = (val: number) => {
  data.query.size = val
}

const exportXlsx = async () => {
  let query = JSON.parse(JSON.stringify(data.query))
  query.page = 1
  query.size = 100000
  let res = await getAgentDailyReport(query)
  let dataList = res.data.list

  let header: string[] = []
  let list: never[][] = []

  data.fields.forEach((item) => {
    header.push(item.label)
  })
  dataList.forEach((item: { [x: string]: any }, index: any) => {
    let arr: never[] = []
    data.fields.forEach((x) => {
      switch (x.prop) {
        case 'date':
          arr.push(setTruncationTime(setTimestampTime(item[x.prop])) as never)
          break
        case 'game_bet':
          arr.push(BigWan(item[x.prop]) as never)
          break
        case 'game_win':
          arr.push(BigWan(item[x.prop]) as never)
          break
        case 'game_income':
          arr.push(BigWan(item[x.prop]) as never)
          break
        default:
          arr.push(item[x.prop] as never)
          break
      }
    })
    list.push(arr)
  })

  exportToExcel(list, header, t('11'))
}

// 重置表头
const resetData = (val: any) => {
  data.fields = val
}
</script>

<style scoped lang="scss"></style>
